"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});
var xhrSend = exports.xhrSend = function xhrSend() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        _ref$timeout = _ref.timeout,
        timeout = _ref$timeout === undefined ? 60000 : _ref$timeout,
        method = _ref.method,
        body = _ref.body,
        url = _ref.url,
        _ref$headers = _ref.headers,
        headers = _ref$headers === undefined ? {} : _ref$headers;

    var xhr = arguments[1];

    return new Promise(function (resolve, reject) {
        xhr.onreadystatechange = function () {
            if (xhr.readyState !== 4) {
                return;
            }
            var res = {
                status: xhr.status,

                body: xhr.responseText
            };
            if (xhr.status >= 200 && xhr.status < 300) {
                resolve(res);
                return;
            }
            reject(res);
        };
        xhr.onerror = reject;
        xhr.timeout = timeout;

        xhr.open(method, url, true);
        for (var k in headers) {
            xhr.setRequestHeader(k, headers[k]);
        }
        xhr.send(body);
    });
};
exports.default = xhrSend;